package com.lmc.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.lmc.R;
import com.lmc.SkillsDetailsActivity;
import com.lmc.entity.Skills;
import com.lmc.util.GlideUtil;

import java.util.List;

public class HomeSelectPagerAdapter extends PagerAdapter {
    private Context mContext;
    private List<Skills> lists;

    public HomeSelectPagerAdapter(Context mContext, List<Skills> lists) {
        this.mContext = mContext;
        this.lists = lists;
    }


    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.lmc_home_select_pager_list_item, null);
        ImageView title = view.findViewById(R.id.title);
        switch (position) {
            case 0:
                title.setImageResource(R.drawable.lmc_skill_1);
                break;
            case 1:
                title.setImageResource(R.drawable.lmc_skill_2);
                break;
            case 2:
                title.setImageResource(R.drawable.lmc_skill_3);
                break;
        }

        ListView listView = view.findViewById(R.id.listView);
        listView.setAdapter(new Adapter(lists.get(position).getData()));
        container.addView(view);
        return view;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }


    private class Adapter extends BaseAdapter {
        List<Skills.SkillsBen> list;

        public Adapter(List<Skills.SkillsBen> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Skills.SkillsBen getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(mContext, R.layout.lmc_home_select_item_list_item, null);
                holder = new ViewHolder();
                holder.thumbnail = convertView.findViewById(R.id.thumbnail);
                holder.name = convertView.findViewById(R.id.name);
                holder.text1 = convertView.findViewById(R.id.text1);
                holder.text2 = convertView.findViewById(R.id.text2);
                holder.line = convertView.findViewById(R.id.line);
                holder.layout = convertView;
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            final Skills.SkillsBen ben = getItem(position);
            GlideUtil.setImg(mContext, holder.thumbnail, ben.getIcon());
            holder.name.setText(ben.getName());
            try {
                String[] split = ben.getVoice().split(",");
                holder.text1.setText(split[0]);
                holder.text2.setText(split[1]);
            }catch (Exception e){

            }


            if (position == list.size() - 1) {
                holder.line.setVisibility(View.GONE);
            } else {
                holder.line.setVisibility(View.VISIBLE);
            }

            holder.layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //进入技能详情
                    SkillsDetailsActivity.start(mContext,ben.getId() + "");
                }
            });
            return convertView;
        }

        private class ViewHolder {
            ImageView thumbnail;
            TextView name;
            TextView text1;
            TextView text2;
            View line;
            View layout;
        }
    }
}
